﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Text;
using System.Threading;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using Hong.Common.Encryption;

namespace Hong.Consumer
{
    public class Program
    {

        #region RebbitMQ消费者
        /// <summary>
        /// 消费
        /// </summary>
        /// <param name="args"></param>
        private static SendEmail test;

        private static string mes;

        static void Main(string[] args)
        {
            var factory = new ConnectionFactory() { };
            factory.HostName = "10.31.60.17"; //RabbitMQ服务在本地运行
            factory.UserName = "huskie"; //用户名
            factory.Password = "123456"; //密码
            factory.Port = AmqpTcpEndpoint.UseDefaultPort;
            //factory.VirtualHost = "/";  //默认为"/"
            using (var connection = factory.CreateConnection())
            using (var channel = connection.CreateModel())
            {
                channel.ExchangeDeclare(exchange: "logs", type: "fanout");
                var queueName = channel.QueueDeclare().QueueName;
                channel.QueueBind(queue: queueName,
                    exchange: "logs",
                    routingKey: "");
                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (model, ea) =>
                {
                    var body = ea.Body;
                    var message = Encoding.UTF8.GetString(body.ToArray());
                        //发送邮件
                        mes = "";
                    mes = message;
                    OnTimedEvent();
                    Console.WriteLine("已发送     -{0}", DateTime.Now);

                };
                channel.BasicConsume(queue: queueName,
                    autoAck: true,
                    consumer: consumer);
                Console.ReadKey();
            }
        }

        private static void OnTimedEvent() //Object source, ElapsedEventArgs e
        {
            Console.WriteLine("正在发送     -{0}", DateTime.Now);
            Thread t = new Thread(testFunc);
            t.Start();

        }

        private static void testFunc()
        {
            test = new SendEmail();
            test.Excute();
        }


        /// <summary>
        /// 仅支持QQ邮箱
        /// </summary>
        public class SendEmail
        {
            /// <summary>
            /// 发送方邮箱
            /// </summary>
            public string fromEmail { get; set; }

            /// <summary>
            /// 发送方邮箱密码
            /// </summary>
            public string fromEmailPwd { get; set; }

            /// <summary>
            /// 接收方邮箱列表
            /// </summary>
            public List<string> toEmails { get; set; }

            /// <summary>
            /// 发送内容
            /// </summary>
            public string content { get; set; }

            public SendEmail()
            {
                fromEmail = "2201170426@qq.com";
                fromEmailPwd = "ubuotnulwicfdige"; //授权码
                toEmails = new List<string>() { "xuan497294138@163.com" };
                content = "警告----";
            }

            public void Excute()
            {
                MailAddress from = new MailAddress(fromEmail);
                MailMessage message = new MailMessage();
                message.Body = content + mes;
                message.IsBodyHtml = true;
                message.BodyEncoding = System.Text.Encoding.UTF8;

                foreach (var item in toEmails)
                {
                    message.To.Add(item);
                }

                message.Subject = "警告!";
                message.SubjectEncoding = System.Text.Encoding.UTF8;
                message.From = from;
                SmtpClient client = new SmtpClient();
                client.EnableSsl = true;
                client.Host = "smtp.qq.com";
                client.Port = 587;

                //邮箱账户和密码
                client.Credentials =
                    new System.Net.NetworkCredential(fromEmail,
                        fromEmailPwd); //邮件服务器验证信息， qq邮箱授权码通过设置->账户->开启服务：POP3/SMTP服务获取
                try
                {
                    client.Send(message);

                }
                catch (Exception ex)
                {
                    string mssage = ex.ToString();

                }
            }
        }
        #endregion

        #region 加解密

        #endregion

    }
}

